<?php

//用户模型

class User extends CActiveRecord{

    public  $password2;

    //获得数据模型方法
    public static function model($className = __CLASS__){
        return parent::model($className);
    }
    public function  tableName(){
        return "{{user}}";
    }
    //对应标签名字
    function attributeLabels(){
        return array(
            'username'=>'用户名',
            'password'=>'密码',
            'password2'=>'确认密码',
            'user_email'=>'邮箱',
            'user_qq'=>'QQ',
            'user_tel'=>'电话',
            'user_sex'=>'性别',
            'user_xueli'=>'学历',
            'user_hobby'=>'爱好',
            'user_introduce'=>'简介',
        );
    }

    //表单验证
    public  function rules(){
        return array(
            array('username','required','message'=>'用户名必填'),

            array('password','required','message'=>'密码必填'),
            array('password2','compare','compareAttribute'=>'password','message'=>'密码必须一致'),

            array('user_email','email','allowEmpty'=>false,'message'=>'邮箱格式不对'),
            array('user_qq','match','pattern'=>'/^[1-9]\d{4,11}/','message'=>'QQ格式不正确'),
            array('user_tel','match','pattern'=>'/^1\d{10}/','message'=>'手机号码不正确'),
            array('user_xueli','match','pattern'=>'/[2-5]/','message'=>'请选择学历'),
            array('user_hobby','check_bobby'),
            //为没有具体验证的规则的属性，设置安全的验证规则，否则attribute不给接受信息
            array('user_sex,user_introduce','safe'),
        );
    }
    //自动以爱好验证方法
    function  check_bobby(){
//        echo $this->user_hobby;
        $len = strlen($this->user_hobby);
        if($len<3){
            $this->addError('user_hobby','爱好必须选择两项或两项以上');
        }
    }

}